﻿@{
    ViewBag.Title = "新增/修改";
    Layout = "~/Views/Shared/_Form.cshtml";
}
<script src="~/Content/js/jquery.form.js"></script>
<style>
    .formTitle span {
        color: red;
    }

    .tab-content #basicInfo table tr td {
        border: 0;
    }

    #myTab li {
        padding: 2px 2px 0 2px;
    }

    #myTab a {
        padding: 8px;
        width: 65px;
        text-align: center;
    }

    span[name=dw] {
        line-height: 26px;
    }
</style>

<form id="form1" method="post" enctype="multipart/form-data" acton="/ProductManage/Product/UploadImag">
    <div class="container" style="padding:0;">
        <ul class="nav nav-tabs" role="tablist" id="myTab">
            <li role="presentation"><a href="#basicInfo" role="tab" data-toggle="tab" style="width: 80px;" id="li1">基本信息</a></li>
            <li role="presentation"><a href="#unit" role="tab" data-toggle="tab" style="width: 80px;" id="li2">可用单位</a></li>
            <li role="presentation"><a href="#addUnit" role="tab" data-toggle="tab" style="width: 80px;" id="li3">增加单位</a></li>
        </ul>
        <div class="tab-content" style="margin-top:10px;overflow-y: auto;">
            <div id="basicInfo" role="tabpanel" class="tab-pane fade in active">
                <table class="form" style="width: 97%;">
                    <tr>
                        <th class="formTitle"><span class="required">*</span>物资名称：</th>
                        <td class="formValue">
                            <input id="name" name="name" type="text" class="form-control required" placeholder="请输入名称" />
                        </td>
                        <th class="formTitle"><span class="required">*</span>首拼：</th>
                        <td class="formValue">
                            <input id="py" name="py" type="text" class="form-control" />
                        </td>
                        <td colspan="2" rowspan="5" align="center">
                            <img id="imageUrl" src="~/Content/img/productDefault.png" style="width:240px; height:175px;" />
                        </td>
                    </tr>
                    <tr>
                        <th class="formTitle"><span class="required">*</span>类别：</th>
                        <td class="formValue" colspan="3">
                            <select id="typeId" name="typeId" class="form-control">
                                <option value="">==请选择==</option>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <th class="formTitle"><span class="required">*</span>最小单位：</th>
                        <td class="formValue">
                            <div style="width:85px;float:left;margin-right:5px;">
                                <select id="minUnit" name="minUnit" class="form-control">
                                    <option value="">=请选择=</option>
                                </select>
                            </div>
                            <a class="btn btn-primary" onclick="ToAddUnit()"><i class="fa fa-plus"></i></a>
                        </td>
                        <th class="formTitle"><span class="required">*</span>进价：</th>
                        <td class="formValue">
                            <input id="jj" name="jj" type="text" class="form-control" style="width:70px;float:left" />
                            <span style="margin-left: 2px;line-height: 26px;">元 /</span>
                            <span name="dw"></span>
                        </td>
                    </tr>
                    <tr>
                        <th class="formTitle"><span class="required">*</span>零售价：</th>
                        <td class="formValue">
                            <input id="lsj" name="lsj" type="text" class="form-control" style="width:70px;float:left" />
                            <span style="margin-left: 2px;line-height: 26px;">元 /</span>
                            <span name="dw"></span>
                        </td>
                        <th class="formTitle"><span class="required">*</span>物资代码：</th>
                        <td class="formValue">
                            <input id="productCode" name="productCode" readonly="readonly" type="text" class="form-control" />
                        </td>
                    </tr>
                    <tr>
                        <th class="formTitle">生产商：</th>
                        <td class="formValue" colspan="3">
                            <input id="supplierName" name="supplierName" type="text" class="form-control" />
                            <input id="supplierId" name="supplierId" value="" type="hidden" />
                            @*<select id="supplierId" name="supplierId" class="form-control">
                    <option value="">==请选择==</option>
                </select>*@
                        </td>
                    </tr>
                    <tr>
                        <th class="formTitle">物资图片：</th>
                        <td class="formValue" colspan="3">
                            <input id="imgUpload" name="imgUpload" accept="image/*" type="file" style="float:left;" />
                        </td>
                        <th class="formTitle">品牌：</th>
                        <td class="formValue">
                            <input id="brand" name="brand" type="text" class="form-control" />
                        </td>
                    </tr>
                    <tr>
                        <th class="formTitle">最小起订数：</th>
                        <td class="formValue">
                            <input id="zxqds" name="zxqds" type="text" class="form-control" style="width:70px; float: left; margin-right: 10px;" />
                            <span name="dw"></span>
                        </td>
                        <th class="formTitle">注册证号：</th>
                        <td class="formValue">
                            <input id="zczh" name="zczh" type="text" class="form-control" />
                        </td>
                        <th class="formTitle">型号规格：</th>
                        <td class="formValue">
                            <input id="gg" name="gg" type="text" class="form-control" />
                        </td>
                    </tr>
                    <tr>
                        <th class="formTitle" style="height: 35px;">是否复用：</th>
                        <td class="formValue" style="padding-top: 1px;">
                            <div class="ckbox">
                                <input id="sffy" name="sffy" type="checkbox"><label for="sffy">是</label>
                            </div>
                        </td>
                        <th class="formTitle" style="height: 35px;">是否跟台：</th>
                        <td class="formValue" style="padding-top: 1px;">
                            <div class="ckbox">
                                <input id="sfgt" name="sfgt" type="checkbox"><label for="sfgt">是</label>
                            </div>
                        </td>
                        <th class="formTitle" style="height: 35px;">是否零库存：</th>
                        <td class="formValue" style="padding-top: 1px;">
                            <div class="ckbox">
                                <input id="sflkc" name="sflkc" type="checkbox"><label for="sflkc">是</label>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <th class="formTitle">自负性质：</th>
                        <td class="formValue">
                            @*<input id="zfxz" name="zfxz" type="text" class="form-control" />*@
                            @Html.DropDownList("zfxz", Newtouch.Herp.Infrastructure.Enum.EnumZFXZv2.ZF.ToDescSelectList(), "==请选择==", new { @class = "form-control required" })
                        </td>
                        <th class="formTitle">自付比例：</th>
                        <td class="formValue">
                            <input id="zfbl" name="zfbl" type="text" class="form-control" />
                        </td>
                        <th class="formTitle">省医保代码：</th>
                        <td class="formValue">
                            <input id="ybdm" name="ybdm" type="text" class="form-control" />
                        </td>
                    </tr>
                    <tr>
                        <th class="formTitle">国家医保代码：</th>
                        <td class="formValue" colspan="2">
                            <input id="gjybdm" name="gjybdm" type="text" class="form-control" />
                        </td>
                        <th class="formTitle">细分代码(采购)：</th>
                        <td class="formValue" colspan="2">
                            <input id="hcgjybdm" name="hcgjybdm" type="text" class="form-control" />
                        </td>

                    </tr>
                    <tr>
                        <th class="formTitle">账簿类别：</th>
                        <td class="formValue">
                            <input id="zblb" name="zblb" type="text" class="form-control" />
                        </td>
                        <th class="formTitle">核算类别：</th>
                        <td class="formValue">
                            <input id="hslb" name="hslb" type="text" class="form-control" />
                        </td>
                    </tr>
                    <tr>
                        <th class="formTitle" style="height: 35px;">选项：</th>
                        <td class="formValue" style="padding-top: 1px;" colspan="5">
                            <div class="ckbox">
                                <input id="zt" name="zt" type="checkbox" checked="checked"><label for="zt">有效</label>
                            </div>
                            <div class="ckbox">
                                <input id="iswzsame" name="iswzsame" type="checkbox"><label for="iswzsame">医用耗材</label>
                            </div>
                        </td>
                    </tr>
                </table>
            </div>
            <div id="unit" role="tabpanel" class="tab-pane fade in">
                <table class="form" style="width: 95%;">
                    <tr>
                        <th class="formTitle"><span class="required">*</span>单位：</th>
                        <td class="formValue">
                            <select id="unitId" name="unitId" class="form-control">
                                <option value="">==请选择==</option>
                            </select>
                            <input type="hidden" id="hdId" value="" />
                        </td>
                        <th class="formTitle"><span class="required">*</span>转化因子：</th>
                        <td class="formValue">
                            <input id="zhyz" name="zhyz" type="text" class="form-control" />
                        </td>
                    </tr>
                    <tr>
                        <td class="formTitle" colspan="4">
                            <input type="button" id="btn_clean" class="btn btn-md btn-primary" style="width:50px; margin-left:40px;" value="清除" />
                            <input type="button" id="btn_submit" class="btn btn-md btn-primary" style="width:50px; margin-left:10px;" value="确定" />
                        </td>
                    </tr>
                </table>
                <div style="margin:10px 5px 0 5px;">
                    <table id="gridList"></table>
                </div>
            </div>
            <div id="addUnit" role="tabpanel" class="tab-pane fade in">
                <table class="form" style="width: 95%;">
                    <tr>
                        <th class="formTitle"><span class="required">*</span>单位名称：</th>
                        <td class="formValue">
                            <input id="UnitName" name="UnitName" type="text" class="form-control required" placeholder="请输入名称" />
                        </td>
                    </tr>
                    <tr>
                        <th class="formTitle">备注：</th>
                        <td class="formValue">
                            <input id="remark" name="remark" type="text" class="form-control" />
                        </td>
                    </tr>
                    <tr>
                        <th class="formTitle" style="height: 35px;">选项：</th>
						<td class="formValue" style="padding-top: 1px;">
							<div class="ckbox">
								<input id="UnitZt" name="UnitZt" type="checkbox" checked="checked"><label for="UnitZt">有效</label>
							</div>
						</td>
                    </tr>
                    <tr>
                        <td class="formTitle">
                            <input type="button" id="btn_add" class="btn btn-md btn-primary" style="width:50px; margin-left:40px;" value="添加" />
                            <input type="button" id="btn_cancel" class="btn btn-md btn-primary" style="width:50px; margin-left:10px;" value="取消" />
                        </td>
                    </tr>
                </table>
            </div>
        </div>
    </div>
</form>
<script lang="javascript">
    var keyWord = $.request("keyValue");
    var $gridList = $("#gridList");
    $(".tab-content").height($(document).height() - document.getElementById("myTab").offsetTop - 50);
    $(function () {
        initControl();
        if (!!keyWord) {
            $.najax({
                url: "/ProductManage/Product/GetFormJson",
                data: { keyWord: keyWord },
                dataType: "json",
                success: function (data) {
                    FillInput(data);
                }
            });
        }
        else{
            GetProductCode();
        }
    });
    //填充物资信息
    function FillInput(data) {
        $("#name").val(data.name);
        $("#py").val(data.py);
        $("#typeId").val(data.typeId).trigger("change");
        $("#minUnit").val(data.minUnit).trigger("change");
        $("#jj").val(data.jj);
        $("#lsj").val(data.lsj);
        $("#brand").val(data.brand);
        $("#supplierId").val(data.supplierId);
        $("#supplierName").val(data.supplierName);
        $("#zxqds").val(data.zxqds);
        $("#zczh").val(data.zczh);
        $("#gg").val(data.gg);
        $("#sffy").attr("checked", data.sffy == "1" ? true : false);
        $("#sfgt").attr("checked", data.sfgt == "1" ? true : false);
        $("#sflkc").attr("checked", data.sflkc == "1" ? true : false);
        $("#zt").attr("checked", data.zt == "1" ? true : false);
		$("#productCode").val(data.productCode);
		$("#hcgjybdm").val(data.hcgjybdm);
		$("#iswzsame").attr("checked", data.iswzsame == "1" ? true : false);
		$("#zfxz").val(data.zfxz);
		$("#zfbl").val(data.zfbl == null ? "1":data.zfbl);
		$("#gjybdm").val(data.gjybdm);
        $("#ybdm").val(data.ybdm);
        $("#zblb").val(data.zblb);
        $("#hslb").val(data.hslb);
        if (data.imageUrl !== "") {
            $("#imageUrl").attr("src", data.imageUrl);
        }
    }

    //初始化
    function initControl() {
        $('#myTab a:first').tab('show');
        $('#name').keyup(function () {
            $('#py').val($(this).toShouPin());
        });
        $("#typeId").bindSelect({
            url: "/ProductManage/ProductType/GetPatientTreeSelectJson"
        });
        $("#minUnit").bindSelect({
            url: "/ProductManage/ProductUnit/GetProductUnitSelectJson",
            id: "Id",
            text: "name"
        });
        $("#unitId").bindSelect({
            url: "/ProductManage/ProductUnit/GetProductUnitSelectJson",
            id: "Id",
            text: "name"
        });
        $("#btn_submit").click(function () {
            SubmitUnit();
        });
        $("#btn_clean").click(function () {
            CleanUnitInput();
        });
        $("#btn_add").click(function () {
            AddUnitI();
        });
        $("#btn_cancel").click(function () {
            $("#li1").click();
        });
        $("#minUnit").change(function (e) {
            $("[name=dw]").html($(this).find("option:selected").text());
        });
        $("#imgUpload").change(function (e) {
            var file = e.delegateTarget.files[0];
            if (file.name == "")
            {
            }
            else
            {
                var n1 = file.name.lastIndexOf('.') + 1;
                var fileExt = file.name.substring(n1, n1 + 3).toLowerCase();
                if (fileExt !== "jpg" && fileExt !== "bmp" && fileExt !== "png") {
                    $.modalAlert("目前系统仅支持jpg、bmp、png后缀图片上传!", 'warning');
                    $("#imgUpload").val("");
                    return false;
                }
                if (file.size/1024/2014 > @ViewData["maxSize"])
                {
                    $.modalAlert("图片大小不能超过"+@ViewData["maxSize"]+"M!", 'warning');
                    $("#imgUpload").val("");
                    return false;
                }
            }
            var reader = new FileReader();
            reader.readAsDataURL(file);
            reader.onload = function (ret) {
                //预览图片
                $("#imageUrl").attr({ "src": reader.result });
            }
        });
        GetUnit();
    }

    //提交
    function AcceptClick(callback) {
		var form = $("#form1");
		//console.log("选中：",document.getElementById("iswzsame").checked);
        var postData = {
            zt: document.getElementById("zt").checked,
            sflkc: document.getElementById("sflkc").checked,
            sfgt: document.getElementById("sfgt").checked,
            sffy: document.getElementById("sffy").checked,
            name: $("#name").val(),
            py: $("#py").val(),
            typeId: $("#typeId").val(),
            minUnit: $("#minUnit").val(),
            jj: $("#jj").val(),
            lsj: $("#lsj").val(),
            brand: $("#brand").val(),
            supplierId: $("#supplierId").val(),
            supplierName: $("#supplierName").val(),
            zxqds: $("#zxqds").val(),
            zczh: $("#zczh").val(),
			gg: $("#gg").val(),
			hcgjybdm: $("#hcgjybdm").val(),
			iswzsame: document.getElementById("iswzsame").checked,
			zfxz : $("#zfxz").val(),
			zfbl : $("#zfbl").val(),
			gjybdm : $("#gjybdm").val(),
            ybdm: $("#ybdm").val(),
            zblb: $("#zblb").val(),
            hslb: $("#hslb").val(),
        };
        var options = {
            url: '/ProductManage/Product/SubmitForm?keyValue=' + keyWord,
            type: 'post',
            data: postData,            dataType:'json',            beforeSubmit: function () {
                if (!DataValidate()) {
                    return false;
                }
                return true;
            },
            success: function (data) {
                if(data.state=='success')
                {
                    $.modalMsg(data.message,data.state,1500);
                }
                else
                {
                    $.modalAlert(data.message,data.state);
                }
                callback();
            }
        };
        form.ajaxSubmit(options);
    }

    //数据效验
    function DataValidate() {
        if ($("#name").val() == "") {
            $.modalAlert("物资名称必填", 'warning');
            return false;
        }
        if ($("#py").val() == "") {
            $.modalAlert("拼音必填", 'warning');
            return false;
        }
        if ($("#typeId").val() == "") {
            $.modalAlert("请选择列别", 'warning');
            return false;
        }
        if ($("#minUnit").val() == "") {
            $.modalAlert("请选择最小单位", 'warning');
            return false;
        }
        if ($("#jj").val() == "") {
            $.modalAlert("进价必填", 'warning');
            return false;
        }
        if ($("#lsj").val() == "") {
            $.modalAlert("零售价必填", 'warning');
            return false;
        }
        var strs = $("#imgUpload").val();
        if (strs !== "") {
            var n1 = strs.lastIndexOf('.') + 1;
            var fileExt = strs.substring(n1, n1 + 3).toLowerCase();
            if (fileExt !== "jpg" && fileExt !== "bmp" && fileExt !== "png") {
                $.modalAlert("目前系统仅支持jpg、bmp、png后缀图片上传!", 'warning');
                return false;
            }
        }
        return true;
    }

    //clean unit input
    function CleanUnitInput() {
        $("#zhyz").val("");
        $("#hdId").val("");
        $("#unitId").val("").trigger("change");
    }

    //get unit grid list
    function GetUnit() {
        $gridList.dataGrid({
            url: "/ProductManage/Product/GetProductUnitGridJson",
            height: $(window).height() - 180,
            postData: { keyWord: keyWord },
            colModel: [
                { label: "Id", name: "Id", hidden: true, key: true },
                { label: 'OrganizeId', name: 'OrganizeId', hidden: true },
                { label: 'unitId', name: 'unitId', hidden: true },
                { label: '单位', name: 'unit', width: 250, align: 'left' },
                { label: '转化因子', name: 'zhyz', width: 250, align: 'left' },
                {
                    label: '操作', name: '操作', width: 190, align: 'center', formatter: function (cellvalue, options, rowObject) {
                        return "<input type=\"button\" class=\"btn btn-default btn-md\" value=\"删除\" onclick=\"DeleteUnit('" + rowObject.Id + "');\" />";
                    }
                }
            ],
            multiselect: false,
            rownumbers: false
        });
    }

    //load unit grid data
    function LoadUnit() {
        $gridList.jqGrid("clearGridData"); //清除明细的grid
        $gridList.jqGrid('setGridParam',
            {
                postData: { keyWord: keyWord }
            }).trigger('reloadGrid');
        CleanUnitInput();
    }

    //update unit
    function btn_edit() {
        $("#zhyz").val($("#gridList").jqGridRowValue().zhyz);
        $("#hdId").val($("#gridList").jqGridRowValue().Id);
        $("#unitId").val($("#gridList").jqGridRowValue().unitId).trigger("change");
    }

    //delete unit
    function DeleteUnit(p) {
        $.najax({
            type: "Post",
            url: "/ProductManage/Product/DeleteProductUnit",
            data: { relId: p },
            dataType: "json",
            success: function (data) {
                LoadUnit();
            }
        });
    }

    //add unit
    function SubmitUnit() {
        if (keyWord == "") {
            $.modalAlert("请提交物资基本信息后在修改物资单位", 'warning');
            return false;
        }
        if ($("#contactName").val() == "") {
            $.modalAlert("单位必填", 'warning');
            return false;
        }
        if ($("#telphone").val() == "") {
            $.modalAlert("转化因子必填", 'warning');
            return false;
        }
        var param = {
            unitId: $("#unitId").val(),
            unitName: $("#unitId").find("option:selected").text(),
            zhyz: $("#zhyz").val(),
            keyWord: keyWord,
            id: $("#hdId").val()
        };
        $.najax({
            type: "Post",
            url: "/ProductManage/Product/SubmitUnit",
            data: param,
            dataType: "json",
            success: function (data) {
                LoadUnit();
            }
        });
        return true;
    }

    //增加单位
    function AddUnitI(){
        if ($("#UnitName").val() == "") {
            $.modalAlert("单位名称必填", 'warning');
            return false;
        }
        var keyWord = "";
        var param = {
            name: $("#UnitName").val(),
            remark:$("#remark").val(),
            zt: document.getElementById("UnitZt").checked,
        };
        $.najax({
            type: "Post",
            url: "/ProductManage/ProductUnit/SubmitForm?keyWord=" + keyWord,
            data: param,
            dataType: "json",
            success: function (data) {
                $("#li1").click();
                $.reload()
            }
        });
    }

    //跳转到增加单位
    function ToAddUnit()
    {
        $("#li3").click();
    }

    //获取物资代码
    function GetProductCode()
    {
        $.najax({
            url: "/ProductManage/Product/GetProductCode" ,
            dataType: "text",
            success: function (data) {
                $('#productCode').val(data);
            }
        });
    }

    //生产商
    $("#supplierName").newtouchBatchFloatingSelector({
        clickautotrigger: true,
        height: 145,
        width: 365,
        ajaxmethod: "Get",
        url: '/SupplierManage/Supplier/GetProducers',
        ajaxreqdata: function () {
            var reqData = {};
            reqData.keyword = $("#supplierName").val();
            return reqData;
        },
        colModel: [
            { label: '生产商', name: 'name', width: 350 },
            { label: 'Id', name: 'Id', hidden: true }
        ],
        itemdbclickhandler: function ($thistr) {
            $("#supplierName").val($thistr.attr('data-name'));
            $("#supplierId").val($thistr.attr('data-Id'));
        }
    });

    //点击物资代码输入框时
    $('#productCode').focus(function () {
        if ($.trim($(this).val()) == '') {
            GetProductCode();
        }
    });
    //检查物资代码是否存在
    $('#productCode').blur(function () {
        if ($.trim($(this).val()) != '') {
            $.najax({
                url: "/ProductManage/Product/CheckProductCode" ,
                data: { productCode:$.trim($(this).val()),keyWord: keyWord },
                dataType: "text",
                success: function (data) {
                    if(data!='')
                    {
                        $.modalAlert("物资代码 "+data+" 已存在", 'warning');
                        $('#productCode').val("");
                    }
                }
            });
        }
    });
</script>
